

package com.loong.loong.hik.controller;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.loong.loong.common.core.util.R;
import com.loong.loong.common.excel.annotation.ResponseExcel;
import com.loong.loong.common.log.annotation.SysLog;
import com.loong.loong.hik.entity.HikCdcPublicBroadcast;
import com.loong.loong.hik.service.HikCdcPublicBroadcastService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 公共广播系统
 *
 * @author jinyiang
 * @date 2023-04-26 10:28:06
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/hikcdcpublicbroadcast" )
@Api(value = "hikcdcpublicbroadcast", tags = "公共广播系统管理")
public class HikCdcPublicBroadcastController {

    private final  HikCdcPublicBroadcastService hikCdcPublicBroadcastService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param hikCdcPublicBroadcast 公共广播系统
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page" )
    @PreAuthorize("@pms.hasPermission('hik_hikcdcpublicbroadcast_view')" )
    public R getHikCdcPublicBroadcastPage(Page page, HikCdcPublicBroadcast hikCdcPublicBroadcast) {
		return R.ok(hikCdcPublicBroadcastService.page(page,
				Wrappers.query(hikCdcPublicBroadcast).lambda()
						.like(StringUtils.isNotEmpty(hikCdcPublicBroadcast.getLikeName()), HikCdcPublicBroadcast::getProgramName,hikCdcPublicBroadcast.getLikeName())
						.between(StringUtils.isNotEmpty(hikCdcPublicBroadcast.getStartTime())&&StringUtils.isNotEmpty(hikCdcPublicBroadcast.getEndTime()),HikCdcPublicBroadcast::getCreateTime,hikCdcPublicBroadcast.getStartTime(),hikCdcPublicBroadcast.getEndTime())
						.between(StringUtils.isNotEmpty(hikCdcPublicBroadcast.getStartTime2())&&StringUtils.isNotEmpty(hikCdcPublicBroadcast.getEndTime2()),HikCdcPublicBroadcast::getPublishTime,hikCdcPublicBroadcast.getStartTime2(),hikCdcPublicBroadcast.getEndTime2()))
		);
    }


    /**
     * 通过id查询公共广播系统
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('hik_hikcdcpublicbroadcast_view')" )
    public R getById(@PathVariable("id" ) Integer id) {
        return R.ok(hikCdcPublicBroadcastService.getById(id));
    }

    /**
     * 新增公共广播系统
     * @param hikCdcPublicBroadcast 公共广播系统
     * @return R
     */
    @ApiOperation(value = "新增公共广播系统", notes = "新增公共广播系统")
    @SysLog("新增公共广播系统" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('hik_hikcdcpublicbroadcast_add')" )
    public R save(@RequestBody HikCdcPublicBroadcast hikCdcPublicBroadcast) {
		HikCdcPublicBroadcast hikCdcPublicBroadcast1 = new HikCdcPublicBroadcast();
		hikCdcPublicBroadcast1.setIndexCode(hikCdcPublicBroadcast.getIndexCode());
		long count = hikCdcPublicBroadcastService.count(Wrappers.query(hikCdcPublicBroadcast1));
		if (count>0){
			return R.failed("已存在相同的编号");
		}
        return R.ok(hikCdcPublicBroadcastService.save(hikCdcPublicBroadcast));
    }

    /**
     * 修改公共广播系统
     * @param hikCdcPublicBroadcast 公共广播系统
     * @return R
     */
    @ApiOperation(value = "修改公共广播系统", notes = "修改公共广播系统")
    @SysLog("修改公共广播系统" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('hik_hikcdcpublicbroadcast_edit')" )
    public R updateById(@RequestBody HikCdcPublicBroadcast hikCdcPublicBroadcast) {
		HikCdcPublicBroadcast hikCdcPublicBroadcast1 = new HikCdcPublicBroadcast();
		hikCdcPublicBroadcast1.setId(hikCdcPublicBroadcast.getId());
		HikCdcPublicBroadcast one = hikCdcPublicBroadcastService.getOne(Wrappers.query(hikCdcPublicBroadcast1));
		if (!one.getIndexCode().equals(hikCdcPublicBroadcast.getIndexCode())){
			HikCdcPublicBroadcast hikCdcPublicBroadcast2 = new HikCdcPublicBroadcast();
			hikCdcPublicBroadcast2.setIndexCode(hikCdcPublicBroadcast.getIndexCode());
			long count = hikCdcPublicBroadcastService.count(Wrappers.query(hikCdcPublicBroadcast2));
			if (count>0){
				return R.failed("已存在相同的编号");
			}
		}
        return R.ok(hikCdcPublicBroadcastService.updateById(hikCdcPublicBroadcast));
    }

    /**
     * 通过id删除公共广播系统
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id删除公共广播系统", notes = "通过id删除公共广播系统")
    @SysLog("通过id删除公共广播系统" )
    @DeleteMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('hik_hikcdcpublicbroadcast_del')" )
    public R removeById(@PathVariable Integer id) {
        return R.ok(hikCdcPublicBroadcastService.removeById(id));
    }


    /**
     * 导出excel 表格
     * @param hikCdcPublicBroadcast 查询条件
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('hik_hikcdcpublicbroadcast_export')" )
    public List<HikCdcPublicBroadcast> export(HikCdcPublicBroadcast hikCdcPublicBroadcast) {
        return hikCdcPublicBroadcastService.list(Wrappers.query(hikCdcPublicBroadcast));
    }
}
